﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForqCDocInserting : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //质检单
            UFIDA.U9.QC.QCDocBE.QCDoc qc = key.GetEntity() as UFIDA.U9.QC.QCDocBE.QCDoc;
            if (qc == null) return;

            #region 质检单行新增
            foreach (var line in qc.QCDocLines)
            {
               
                    //引入料品
                    ItemMaster itemMaster = ItemMaster.Finder.Find($"Code='{line.ItemMaster.Code}'");
                    if (itemMaster == null) continue;
                    //判断料品批次检验是否为空
                    if ( itemMaster.DescFlexField.PrivateDescSeg8 != "")
                    {
                    
                    if (itemMaster.DescFlexField.PrivateDescSeg9 == null || itemMaster.DescFlexField.PrivateDescSeg9 == "")
                    {
                        itemMaster.DescFlexField.PrivateDescSeg9 = 1.ToString();
                    }
                 
                        //将料品的最新批次写入质检单最新批次

                        line.DescFlexField.PrivateDescSeg6 = itemMaster.DescFlexField.PrivateDescSeg9;
                    //判断料品最新批次+1<批次检验
                    using (ISession session = Session.Open())
                    {
                        //料品最新批次+1<=料品批次检验
                        if (decimal.Parse(itemMaster.DescFlexField.PrivateDescSeg9) + 1 <= decimal.Parse(itemMaster.DescFlexField.PrivateDescSeg8))
                        {
                            //料品最新批次
                            itemMaster.DescFlexField.PrivateDescSeg9 = (decimal.Parse(itemMaster.DescFlexField.PrivateDescSeg9) + 1).ToString();

                        }

                        else
                        {
                            itemMaster.DescFlexField.PrivateDescSeg9 = 1.ToString();
                        }
                        session.Commit();
                    }
                        //判断最新批次是否为1
                        if (decimal.Parse(line.DescFlexField.PrivateDescSeg6) == 1)
                            line.DescFlexField.PrivateDescSeg5 = "Yes";
                        else
                            line.DescFlexField.PrivateDescSeg5 = "No";
                    }
                    else
                    {
                        return;
                    }

                }
            

            #endregion

        }

    }
}
